package com.linkdood.app.utils;

import com.linkdood.app.constants.LinkDoodConstants;
import com.linkdood.app.domain.ThirdParty;
import com.linkdood.app.service.ddio.DdioUserSyncResponseBody;
import com.linkdood.app.service.ddio.TemplateMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求链接常量
 *
 * @author cfj
 */
@Slf4j
public class DdioUtil {

    private static final String SYNC_ORG_URL = "/sync/v1/syncOrg/";
    private static final String SYNC_USER_URL = "/sync/v1/syncUser/";
    private static final String ALL_ORG_URL = "/org/getAllOrg";
    private static final String ALL_BUDDYS_URL = "/user/allBuddys/";
    private static final String ALL_USER_URL = "/org/getAllUser";
    private static final String OAUTH_TOKEN_URL = "/oauth/token";
    private static final String OAUTH_USER_INFO_URL = "/oauth/getUserInfo";
    private static final String FRESH_ACCESS_TOKEN_URL = "/oauth/freshAccessToken";
    private static final String TOKEN_URL = "/token";
    private static final String USER_INFO_URL = "/user/info/";
    private static final String USER_INFO_BY_IDS = "/user/getUserInfoByUserIDs";

    /**
     * 根据 userIds 获取用户详情 Response
     */
    public static ResponseEntity<String> getUserInfoByIdsResponse(ThirdParty thirdParty, String[] userIds, String token) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(thirdParty.getServerUrl()).append(USER_INFO_BY_IDS);
        Map<String, Object> body = new HashMap<>(1);
        body.put("ids", StringUtils.join(userIds, ","));
        ResponseEntity<String> responseEntity = HttpUtil.postRestTemplate(stringBuilder.toString(), body, getHttpHeader(token));
        printInfo(stringBuilder.toString(), responseEntity);
        return responseEntity;
    }

    /**
     * 获取 /sync/v1/syncOrg/ Response
     */
    public static ResponseEntity<String> getSyncOrgResponse(ThirdParty thirdParty, String version, String token) {
        String url = thirdParty.getServerUrl() + SYNC_ORG_URL + version;
        ResponseEntity<String> responseEntity = HttpUtil.getRestTemplate(url, getHttpHeader(token));
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 /sync/v1/syncUser/ Response
     */
    public static ResponseEntity<String> getSyncUserResponse(ThirdParty thirdParty, String version, String token) {
        String url = thirdParty.getServerUrl() + SYNC_USER_URL + version;
        ResponseEntity<String> responseEntity = HttpUtil.getRestTemplate(url, getHttpHeader(token));
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 /org/getAllOrg Response
     */
    public static ResponseEntity<String> getAllOrgResponse(ThirdParty thirdParty, String token) {
        String url = thirdParty.getServerUrl() + ALL_ORG_URL;
        ResponseEntity<String> responseEntity = HttpUtil.getRestTemplate(url, getHttpHeader(token));
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 /user/allBuddys/ Response
     */
    public static ResponseEntity<String> getAllBuddysResponse(ThirdParty thirdParty, String userToken, String token) {
        String url = thirdParty.getServerUrl() + ALL_BUDDYS_URL + userToken;
        ResponseEntity<String> responseEntity = HttpUtil.getRestTemplate(url, getHttpHeader(token));
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 /org/getAllUser Response
     */
    public static ResponseEntity<String> getAllUserResponse(ThirdParty thirdParty, String token, int pageNum, int pageSize) {
        String url = thirdParty.getServerUrl() + ALL_USER_URL + "?pageNum=" + pageNum + "&pageSize=" + pageSize;
        ResponseEntity<String> responseEntity = HttpUtil.postRestTemplate(url, getHttpHeader(token));
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 /oauth/freshAccessToken Response
     */
    public static ResponseEntity<String> getFreshAccessTokenResponse(ThirdParty thirdParty, String freshToken) {
        String url = thirdParty.getServerUrl() + FRESH_ACCESS_TOKEN_URL + "?appid=" + thirdParty.getAppId() + "&refreshToken=" + freshToken;
        ResponseEntity<String> responseEntity = HttpUtil.postRestTemplate(url);
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取用户信息 Response
     */
    public static ResponseEntity<String> getOauthUserInfoResponse(ThirdParty thirdParty, String token) {
        String url = thirdParty.getServerUrl() + OAUTH_USER_INFO_URL + "?appid=" + thirdParty.getAppId() + "&appsecret=" + HttpUtil.urlEncode(thirdParty.getAppSecret()) + "&token=" + token;
        ResponseEntity<String> responseEntity = HttpUtil.postRestTemplate(url);
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 oauth token Response
     */
    public static ResponseEntity<String> getOauthTokenResponse(ThirdParty thirdParty, String code) {
        String url = thirdParty.getServerUrl() + OAUTH_TOKEN_URL + "?appid=" + thirdParty.getAppId() + "&appsecret=" + HttpUtil.urlEncode(thirdParty.getAppSecret()) + "&code=" + code;
        ResponseEntity<String> responseEntity = HttpUtil.postRestTemplate(url);
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 access_token Response
     */
    public static ResponseEntity<String> getAccessTokenResponse(ThirdParty thirdParty) {
        String url = thirdParty.getServerUrl() + TOKEN_URL + "?accessType=CLIENT&appID=" + thirdParty.getAppId() + "&appSecret=" + HttpUtil.urlEncode(thirdParty.getAppSecret());
        ResponseEntity<String> responseEntity = HttpUtil.getRestTemplate(url);
        printInfo(url, responseEntity);
        return responseEntity;
    }

    /**
     * 获取 user_info Response
     */
    public static ResponseEntity<String> getUserInfoResponse(ThirdParty thirdParty, DdioUserSyncResponseBody.DdioSyncUser user, String token) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(thirdParty.getServerUrl()).append(USER_INFO_URL);
        if (LinkDoodConstants.ACCESS_TYPE_CUSTOM_ACCOUNT.equals(thirdParty.getAccessType())) {
            stringBuilder.append(user.getAccount());
        } else {
            stringBuilder.append(user.getUserId());
        }
        stringBuilder.append("?lang=zh_CN");
        ResponseEntity<String> responseEntity = HttpUtil.getRestTemplate(stringBuilder.toString(), getHttpHeader(token));
        printInfo(stringBuilder.toString(), responseEntity);
        return responseEntity;
    }

    /**
     * 获取包含 access_token 的消息头
     */
    public static HttpHeaders getHttpHeader(String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("access-token", token);
        return headers;
    }


    private static void printInfo(String url, ResponseEntity<String> result) {
        log.debug("request url : {} , result : {} ", url, result);
    }

    /**
     * 获取 message/sendMessageTemplate 接口请求返回
     */
    public static ResponseEntity<String> sendMessage(String url, TemplateMessage templateMessage, HttpHeaders headers) {
        return HttpUtil.postTemplateMessage(url, templateMessage, headers);
    }

    /**
     * 获取 message/sendMessageTemplate 完整接口
     */
    public static String buildSendMessageUrl(ThirdParty thirdParty) {
        String appUrl = thirdParty.getServerUrl();
        return appUrl + "/message/sendMessageTemplate";
    }
}
